'use strict';
var fs = require('fs');
var path = require('path');
var webpack = require('webpack');
var Root_Path = path.resolve(__dirname);
var Build_Path = path.resolve(Root_Path, './build');
var Modules_Path = path.resolve(Root_Path, './node_modules');
var Entry_Path = path.resolve(Root_Path, './entry');
var ServerPort = 8081;
var Glob = require('glob');

var HtmlwebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var openBrowserPlugin = require('open-browser-webpack-plugin');

var plugins = [

    new webpack.DefinePlugin({
        'process.env': {
            PACKAGE_ENV: '"development"',
            //PACKAGE_ENV: JSON.stringify(process.env.PACKAGE_ENV),
            NODE_ENV: '"development"'
        }
    }),

    new webpack.ProvidePlugin({
        $: "jquery",
        _: "lodash",
        API: ["@/api", "default"],
        DZ: ["@/js/utils/app.js", "default"],
        KZ: ["@/js/utils/hybrid-sdk.js", "default"],
    }),

    new webpack.NamedModulesPlugin(),

    new webpack.optimize.CommonsChunkPlugin({
        name: 'common',
        filename: 'js/common.js',
        minChunks: 3
    }),

    new openBrowserPlugin({ url: 'http://localhost:' + ServerPort }),
]

var filenames = Glob.sync(path.resolve(Entry_Path, "**/*.js"));
//var filenames = fs.readdirSync(Entry_Path);
var entryObj = filenames.reduce(function (all, filename) {

    var name = path.basename(filename, path.extname(filename));
    all[name] = filename;

    var htmlPlugin = new HtmlwebpackPlugin({
        template: path.resolve(Root_Path, `./pages/${name}.html`),
        filename: name + ".html",
        chunks: ["common", name],
        hash: true,
    });
    plugins.push(htmlPlugin)

    return all;
}, { "common": ["babel-polyfill", "jquery", "./css/modules/common.scss"] })


//console.log(entryObj)

module.exports = {
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        babelrc: false,
                        presets: ['env', 'stage-0']
                        //plugins: ["transform-remove-strict-mode"]
                    }
                }
            },
            {
                test: /\.css/,
                use: ['style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            sourceMap: true,
                            //modules: true,
                            //localIdentName: "[hash:6]"
                            //localIdentName: "[path]--[name]--[local]--[hash:6]"
                            //src-charge-styles---calendar--calendar--0fda07
                        }
                    },
                    'postcss-loader'
                ]
            },
            {
                test: /\.scss$/,
                use: ['style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            sourceMap: true,
                            //modules: true,
                            //localIdentName: "[hash:6]"
                            //localIdentName: "[path]--[name]--[local]--[hash:6]"
                            //src-charge-styles---calendar--calendar--0fda07
                        }
                    },
                    'postcss-loader',
                    'sass-loader'
                ]
            },
            {
                test: /\.(png|jpeg|jpg|gif)$/,
                use: {
                    loader: "url-loader",
                    options: {
                        limit: 20000,
                        name: "images/[name].[ext]"
                    }
                }
            },
            {
                test: /\.(html|ejs)$/,
                use: ['ejs-compiled-loader']
            },
        ]
    },

    entry: entryObj,

    output: {
        //filename: '[name]/[name].js',
        //chunkFilename: '[name]/[name].js',
        filename: 'js/[name].js',
        chunkFilename: 'js/[name].js',
        path: Build_Path,
        //publicPath: "./"
    },

    devServer: {
        publicPath: '/',
        compress: false,
        hot: true,
        inline: true,
        contentBase: "./",
        host: '0.0.0.0',
        port: ServerPort,
        disableHostCheck: true,

        proxy: {
            "/api": {
                //target: "https://blue-zj-m.weizhipin.com",
                 //target: "https://dianzhangm.zhipin.com/",
                //target: "http://172.16.26.82:8380",
                target: "http://test-m.hippotv.cn/",
                pathRewrite: { "^/api": "" },
                secure: false,
                changeOrigin: true,
                //cookieDomainRewrite: {
                //    "dev.weizhipin.com": "blue-boss.weizhipin.com"
                //}
            }
        }
    },

    resolve: {
        extensions: ['.js', '.scss', '.css', '.json'],
        modules: ["node_modules"],
        alias: {
            '@': path.resolve(__dirname, '')
        }
    },

    plugins: plugins

};